<?php
/**
 * This file manages the actions coming from
 * the admin pages
 *
 * @author University of London Computer Centre
 * @version 1.4.0 - Release Candidate 2
 * @package ArchivePress
 * @subpackage pages
 * @global ArchivePress $ArchivePress
 * @global ArchivePressLog $ArchivePressLog
 */

global $ArchivePress, $ArchivePressLog;

// check if the request is a proper request
if( (count($_GET) == 0) && (count($_POST) == 0) ) {
    exit;
} if(array_key_exists(WP_ARCHIVEPRESS_PREFIX . "action", $_GET)) { // GET actions

    // message to display
    $message = "";

    // ACTION REQUEST
    if(array_key_exists(WP_ARCHIVEPRESS_PREFIX . "action", $_GET)) {
        // switch request
        switch($_GET[WP_ARCHIVEPRESS_PREFIX . "action"]) {
            case "deactivatefeed":      // deactivate feed
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'activate_deactivate_postfeed_' . (int)$_GET["feed_id"]);

                // Do it
                $ArchivePress->setFeed((int)$_GET["feed_id"], "active", "no");
                $message = "&inactive=1&feed_id=".(int)$_GET["feed_id"];
                break;
            case "activatefeed":        // activate feed
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'activate_deactivate_postfeed_' . (int)$_GET["feed_id"]);

                // Do it
                $ArchivePress->setFeed((int)$_GET["feed_id"], "active", "yes");
                $message = "&active=1&feed_id=".(int)$_GET["feed_id"];
                break;
            case "updatefeed":          // update feed
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'update_postfeed_' . (int)$_GET["feed_id"]);

                // Do it
                $feed = $ArchivePress->getFeed((int)$_GET["feed_id"]);
                $res = $ArchivePress->fetchFeed($feed);
                $message = "&fetchedfeed=1&feed_id=".(int)$_GET["feed_id"]."&feeds=".$res;
                break;
            case "updatefeedcomments":  // update comments for a feed
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'updatefeedcomments_postfeed_' . (int)$_GET["feed_id"]);

                // Do it
                $res = $ArchivePress->fetchFeedComments((int)$_GET["feed_id"]);
                $message = "&fetchedcomments=1&feed_id=".(int)$_GET["feed_id"]."&feeds=".$res;
                break;
            case "resetfeed":           // reset feed
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'resetfeed_postfeed_' . (int)$_GET["feed_id"]);

                // Do it
                $ArchivePress->resetFeed((int)$_GET["feed_id"]);
                $message = "&resetfeed=1&feed_id=".(int)$_GET["feed_id"];
                break;
            case "deletefeed":          // delete feed
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'deletefeed_postfeed_' . (int)$_GET["feed_id"]);

                // Do it
                $ArchivePress->deleteFeed((int)$_GET["feed_id"]);
                $message = "&deletefeed=1&feed_id=".(int)$_GET["feed_id"];
                break;

            case "deactivatecommentfeed":      // deactivate feed
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'activate_deactivate_commentfeed_' . (int)$_GET["feed_id"]);

                // Do it
                $ArchivePress->setFeed((int)$_GET["feed_id"], "active", "no");
                $message = "&inactive=1&feed_id=".(int)$_GET["feed_id"];
                break;
            case "activatecommentfeed":        // activate feed
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'activate_deactivate_commentfeed_' . (int)$_GET["feed_id"]);

                // Do it
                $ArchivePress->setFeed((int)$_GET["feed_id"], "active", "yes");
                $message = "&active=1&feed_id=".(int)$_GET["feed_id"];
                break;
            case "updatecommentfeed":          // update feed
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'update_commentfeed_' . (int)$_GET["feed_id"]);

                // Do it
                $res = $ArchivePress->fetchFeedComments((int)$_GET["feed_id"], true, true);
                $message = "&fetchedfeed=1&feed_id=".(int)$_GET["feed_id"]."&feeds=".$res;
                break;

            case "deletelog":          // delete log
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'deletelog_' . (int)$_GET["log_id"]);

                // Do it
                $ArchivePressLog->deleteLog((int)$_GET["log_id"]);
                $message = "&deletelog=1&log_id=".(int)$_GET["log_id"];
                break;

            case "logon":          // log on
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'logon');

                // Do it
                $ArchivePressLog->startLogging();
                update_option(WP_ARCHIVEPRESS_PREFIX . "defLogStatus", ArchivePressLog::LOG_ON);
                $message = "&logon=1";
                break;

            case "logoff":          // log off
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'logoff');

                // Do it
                $ArchivePressLog->stopLogging();
                update_option(WP_ARCHIVEPRESS_PREFIX . "defLogStatus", ArchivePressLog::LOG_OFF);
                $message = "&logoff=1";
                break;

            case "emptylog":          // empty log
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'emptylog');

                // Do it
                $ArchivePressLog->emptyLog();
                $message = "&logempty=1";
                break;


            // FILTERS & BULK ACTIONS
            case "show_postfeeds":      // posts filters
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'show_postfeeds');

                // CHECK FOR BULK ACTIONS
                if( isset($_GET["doaction"]) || isset($_GET["doaction2"])){
                    // manage actions
                    $action = $_GET["action"];
                    if($action == -1) {
                        $action = $_GET["action2"];
                    }
                    for($i = 0; $i < count($_GET["feed"]); $i++) {
                        switch($action) {
                           case "deactivatefeed":      // deactivate feed
                                $ArchivePress->setFeed((int)$_GET["feed"][$i], "active", "no");
                                break;
                            case "activatefeed":        // activate feed
                                $ArchivePress->setFeed((int)$_GET["feed"][$i], "active", "yes");
                                break;
                            case "resetfeed":           // reset feed
                                $ArchivePress->resetFeed((int)$_GET["feed"][$i]);
                                break;
                            case "deletefeed":          // delete feed
                                $ArchivePress->deleteFeed((int)$_GET["feed"][$i]);
                                break;
                        }
                    }
                    // message
                    switch($action) {
                       case "deactivatefeed":      // deactivate feed
                            $message = "&inactive=".count($_GET["feed"]);
                            break;
                        case "activatefeed":        // activate feed
                            $message = "&active=".count($_GET["feed"]);
                            break;
                        case "resetfeed":           // reset feed
                            $message = "&resetfeed=".count($_GET["feed"]);
                            break;
                        case "deletefeed":          // delete feed
                            $message = "&deletefeed=".count($_GET["feed"]);
                            break;
                    }

                }
                // redirect to the proper url
                $url = "";
                foreach($_GET as $key => $value) {
                    if( ($key != "_wpnonce") && ($key != "page") && ($key != "arpr_action") && ($key != "back") && ($key != "action")
                        && ($key != "feed") && ($key != "action2") && ($key != "doaction") && ($key != "doaction2") ){
                        $url .= "&".$key."=".urlencode($value);
                    }
                }

                wp_redirect(get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/'.$_GET["back"].".php".$url.$message);
                exit;
                break;

            case "show_commentfeeds":      // comments filters
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'show_commentfeeds');

                // CHECK FOR BULK ACTIONS
                if( isset($_GET["doaction"]) || isset($_GET["doaction2"])){
                    // manage actions
                    $action = $_GET["action"];
                    if($action == -1) {
                        $action = $_GET["action2"];
                    }
                    for($i = 0; $i < count($_GET["feed"]); $i++) {
                        switch($action) {
                           case "deactivatefeed":      // deactivate feed
                                $ArchivePress->setFeed((int)$_GET["feed"][$i], "active", "no");
                                break;
                            case "activatefeed":        // activate feed
                                $ArchivePress->setFeed((int)$_GET["feed"][$i], "active", "yes");
                                break;
                        }
                    }
                    // message
                    switch($action) {
                       case "deactivatefeed":      // deactivate feed
                            $message = "&inactive=".count($_GET["feed"]);
                            break;
                        case "activatefeed":        // activate feed
                            $message = "&active=".count($_GET["feed"]);
                            break;
                    }

                }
                // redirect to the proper url
                $url = "";
                foreach($_GET as $key => $value) {
                    if( ($key != "_wpnonce") && ($key != "page") && ($key != "arpr_action") && ($key != "back") && ($key != "action")
                        && ($key != "feed") && ($key != "action2") && ($key != "doaction") && ($key != "doaction2") ){
                        $url .= "&".$key."=".urlencode($value);
                    }
                }

                wp_redirect(get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/'.$_GET["back"].".php".$url.$message);
                exit;
                break;

            case "show_logs":      // log filters
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'show_logs');

                // CHECK FOR BULK ACTIONS
                if( isset($_GET["doaction"]) || isset($_GET["doaction2"])){
                    // manage actions
                    $action = $_GET["action"];
                    if($action == -1) {
                        $action = $_GET["action2"];
                    }
                    for($i = 0; $i < count($_GET["log"]); $i++) {
                        switch($action) {
                           case "deletelog":      // deactivate feed
                                $ArchivePressLog->deleteLog((int)$_GET["log"][$i]);
                                break;
                        }
                    }
                    // message
                    switch($action) {
                       case "deletelog":      // delete logs
                            $message = "&deletelog=".count($_GET["log"]);
                            break;
                    }

                }
                // redirect to the proper url
                $url = "";
                foreach($_GET as $key => $value) {
                    if( ($key != "_wpnonce") && ($key != "page") && ($key != "arpr_action") && ($key != "back") && ($key != "action")
                        && ($key != "feed") && ($key != "action2") && ($key != "doaction") && ($key != "doaction2") ){
                        $url .= "&".$key."=".urlencode($value);
                    }
                }

                wp_redirect(get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/'.$_GET["back"].".php".$url.$message);
                exit;
                break;

          case "show_authors":      // feed authors filters
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'show_authors');

                // redirect to the proper url
                $url = "";
                foreach($_GET as $key => $value) {
                    if( ($key != "_wpnonce") && ($key != "page") && ($key != "arpr_action") && ($key != "back") && ($key != "action")
                        && ($key != "feed") && ($key != "action2") && ($key != "doaction") && ($key != "doaction2") ){
                        $url .= "&".$key."=".urlencode($value);
                    }
                }

                wp_redirect(get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/'.$_GET["back"].".php".$url.$message);
                exit;
                break;


            default: exit;
        }

        // redirect to the proper url
        wp_redirect(get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/'.$_GET["back"].".php".$message);

    }

} if(array_key_exists(WP_ARCHIVEPRESS_PREFIX . "action", $_POST)) { // POST actions
    // ACTION REQUEST
    // switch request
    switch($_POST[WP_ARCHIVEPRESS_PREFIX . "action"]) {
        case "editpost":      // edit post feed
            // security check
            check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'edit-post_feed_' . (int)$_POST["feed_id"]);

            // do it
            // name
            $ArchivePress->setFeed((int)$_POST["feed_id"], "name", $_POST[WP_ARCHIVEPRESS_PREFIX . 'feedname']);

            // active link
            $ArchivePress->setActiveLink((int)$_POST["feed_id"], (int)$_POST[WP_ARCHIVEPRESS_PREFIX . 'feedlink']);

            // interval
            $ArchivePress->setFeedOption((int)$_POST["feed_id"], "interval", (int)$_POST[WP_ARCHIVEPRESS_PREFIX . 'feedinterval']);

            // active
            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . 'feedactive'])) {
                $ArchivePress->setFeed((int)$_POST["feed_id"], "active", "yes");
            } else {
                $ArchivePress->setFeed((int)$_POST["feed_id"], "active", "no");
            }

            // comments
            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . 'feedcomments'])) {
                $ArchivePress->setFeedOption((int)$_POST["feed_id"], "comments", "yes");
            } else {
                $ArchivePress->setFeedOption((int)$_POST["feed_id"], "comments", "no");
            }

            // follow permalinks
            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . 'feedpermalink'])) {
                $ArchivePress->setFeedOption((int)$_POST["feed_id"], WP_ARCHIVEPRESS_PREFIX . "followPermaLink", "yes");
            } else {
                $ArchivePress->setFeedOption((int)$_POST["feed_id"], WP_ARCHIVEPRESS_PREFIX . "followPermaLink", "no");
            }

            // embed media
            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . 'feedmedia'])) {
                $ArchivePress->setFeedOption((int)$_POST["feed_id"], WP_ARCHIVEPRESS_PREFIX . "embedMedia", "yes");
            } else {
                $ArchivePress->setFeedOption((int)$_POST["feed_id"], WP_ARCHIVEPRESS_PREFIX . "embedMedia", "no");
            }

            // default post status
            $ArchivePress->setFeedOption((int)$_POST["feed_id"], WP_ARCHIVEPRESS_PREFIX . "defPostStatus", $_POST[WP_ARCHIVEPRESS_PREFIX . 'feedpoststatus']);

            // excerpt
            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . 'feedexcerpt'])) {
                $ArchivePress->setFeedOption((int)$_POST["feed_id"], WP_ARCHIVEPRESS_PREFIX . "tryToFindExcerpt", "yes");
            } else {
                $ArchivePress->setFeedOption((int)$_POST["feed_id"], WP_ARCHIVEPRESS_PREFIX . "tryToFindExcerpt", "no");
            }

            // comment status
            $ArchivePress->setFeedOption((int)$_POST["feed_id"], WP_ARCHIVEPRESS_PREFIX . "defCommentsStatus", $_POST[WP_ARCHIVEPRESS_PREFIX . 'feedcommentstatus']);


            $message = "&update=1&feed_id=".(int)$_POST["feed_id"];
            break;

        case "editcomment":      // edit post feed
            // security check
            check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'edit-comment_feed_' . (int)$_POST["feed_id"]);

            // do it
            // active link
            $ArchivePress->setActiveLink((int)$_POST["feed_id"], (int)$_POST[WP_ARCHIVEPRESS_PREFIX . 'feedlink']);

            // interval
            $ArchivePress->setFeedOption((int)$_POST["feed_id"], "interval", (int)$_POST[WP_ARCHIVEPRESS_PREFIX . 'feedinterval']);

            // active
            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . 'feedactive'])) {
                $ArchivePress->setFeed((int)$_POST["feed_id"], "active", "yes");
            } else {
                $ArchivePress->setFeed((int)$_POST["feed_id"], "active", "no");
            }

            // comment status
            $ArchivePress->setFeedOption((int)$_POST["feed_id"], WP_ARCHIVEPRESS_PREFIX . "areCommentsApproved", $_POST[WP_ARCHIVEPRESS_PREFIX . 'feedcommentapproved']);


            $message = "&update=1&feed_id=".(int)$_POST["feed_id"];
            break;

        case "addfeed":      // add a feed
            // security check
            check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'addfeed');

            // do it
            if(array_key_exists(WP_ARCHIVEPRESS_PREFIX . 'feedurl', $_POST)){
                // "normal" case
                $testURL = $_POST[WP_ARCHIVEPRESS_PREFIX . 'feedurl'];
            } else {
                // multiple url choice
                $testURL = $_POST[WP_ARCHIVEPRESS_PREFIX . 'feedlink'];
            }
            $testNAME = $_POST[WP_ARCHIVEPRESS_PREFIX . 'feedname'];

            // change default feed protocol, according to the advanced settings
            // 1. SAVE OLD
            $oldDefProtocol = get_option(WP_ARCHIVEPRESS_PREFIX . "defFeedProtocol");
            // 2. UPDATE
            update_option(WP_ARCHIVEPRESS_PREFIX . "defFeedProtocol", $_POST[WP_ARCHIVEPRESS_PREFIX . 'feedprotocol']);


            $feedAdded = false;
            $lastSentence = "";
            // ADD NEW FEED PROCEDURE
            // check if is a feed url
            try {
                if($ArchivePress->isFeed($testURL)){
                    try {
                        $ArchivePress->addLastCheckedFeed($testNAME,true);
                        $feedAdded = true;
                    } catch (Exception $e) {
                        $lastSentence = __($e->getMessage(), ARCHIVEPRESS);
                        $feedAdded = false;
                    }
                } else {
                    // try autodiscovery
                    $feeds = array();
                    try {
                        $feeds = FeedAutoDiscovery::getFeedLinks($testURL);
                        // If the feed was not already added
                        if($ArchivePress->isFeedNew($feeds)) {
                            // Add Feed
                            try {
                                if($ArchivePress->addFeed($feeds,$testURL,$testNAME) === true ) {
                                    $feedAdded = true;
                                } else {
                                    // there are more than one feed links
                                    // and it's not possible to choose automatically
                                    $lastSentence = __("There are more than one feed links and it's not possible to choose automatically: please choose a feed url from the list or try with another feed url.", ARCHIVEPRESS);
                                    // save links
                                    $ArchivePress->setTmpOption(WP_ARCHIVEPRESS_PREFIX . "feedlinks", serialize($feeds));
                                    $feedAdded = false;
                                }
                            } catch (Exception $e) {
                                $lastSentence = __($e->getMessage(), ARCHIVEPRESS);
                                $feedAdded = false;
                            }
                        } else {
                            $lastSentence = __("Feed already added!", ARCHIVEPRESS);
                            $feedAdded = false;
                        }
                    } catch (Exception $e) {
                        $lastSentence = __($e->getMessage(), ARCHIVEPRESS);
                        $feedAdded = false;
                    }
                }
            } catch (Exception $e) {
                // page not found or other curl errors
                $lastSentence = __($e->getMessage(), ARCHIVEPRESS);
                $feedAdded = false;
            }

            // check if there was an uploaded file
            // (only if the feed procedure worked properly)
            if($feedAdded) {
                // get feed id
                $feed_id = $ArchivePress->getLastFeedId();

                // check if there was an uploaded file
                if(isset($_FILES[WP_ARCHIVEPRESS_PREFIX . 'feedfile'])) {
                    if($_FILES[WP_ARCHIVEPRESS_PREFIX . 'feedfile']["name"] != "") {
                        // 1. check if is a "text" file
                        if($_FILES[WP_ARCHIVEPRESS_PREFIX . 'feedfile']['type'] == "text/xml"){
                                // 2. check if is a feed format (RSS or ATOM)
                                $content = utf8_encode(file_get_contents($_FILES[WP_ARCHIVEPRESS_PREFIX . 'feedfile']['tmp_name']));
                                try {
                                        if($ArchivePress->isFeed("",$content)){
                                                // PARSE IT!
                                                // temp values
                                                $feed["file"]           = $content;
                                                $feed["protocol"]       = $ArchivePress->lastFeedCheckedProt;
                                                $feed["id"]             = $feed_id;
                                                $feed["name"]           = $testNAME;
                                                $ArchivePress->fetchFeed($feed);

                                                $ArchivePress->setTmpOption(WP_ARCHIVEPRESS_PREFIX . "feedfileuploaded", "yes");
                                        }
                                }catch (Exception $e){
                                    $lastSentence = __($e->getMessage(), ARCHIVEPRESS);
                                    $feedAdded = false;
                                    // delete the feed
                                    $ArchivePress->deleteFeed($feed_id);
                                }

                        } else {
                            $lastSentence = __("File not supported", ARCHIVEPRESS);
                            $feedAdded = false;
                            // delete the feed
                            $ArchivePress->deleteFeed($feed_id);
                        }
                    }
                }
            }
            // CUSTOM ADVANCED OPTIONS
            if($feedAdded) {
                // get feed id
                $feed_id = $ArchivePress->getLastFeedId();

                // fetching interval
                $ArchivePress->setFeedOption($feed_id, "interval", $_POST[WP_ARCHIVEPRESS_PREFIX . 'feedinterval']);

                // fetch also comments
                if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . 'feedcomments'])) {
                    $ArchivePress->setFeedOption($feed_id, "comments", "yes");
                } else {
                    $ArchivePress->setFeedOption($feed_id, "comments", "no");
                }

                // follow permalinks
                if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . 'feedpermalink'])) {
                    $ArchivePress->setFeedOption($feed_id, WP_ARCHIVEPRESS_PREFIX . "followPermaLink", "yes");
                } else {
                    $ArchivePress->setFeedOption($feed_id, WP_ARCHIVEPRESS_PREFIX . "followPermaLink", "no");
                }

                // embed media
                if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . 'feedmedia'])) {
                    $ArchivePress->setFeedOption($feed_id, WP_ARCHIVEPRESS_PREFIX . "embedMedia", "yes");
                } else {
                    $ArchivePress->setFeedOption($feed_id, WP_ARCHIVEPRESS_PREFIX . "embedMedia", "no");
                }

                // default post status
                $ArchivePress->setFeedOption($feed_id, WP_ARCHIVEPRESS_PREFIX . "defPostStatus", $_POST[WP_ARCHIVEPRESS_PREFIX . 'feedpoststatus']);

                // excerpt
                if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . 'feedexcerpt'])) {
                    $ArchivePress->setFeedOption($feed_id, WP_ARCHIVEPRESS_PREFIX . "tryToFindExcerpt", "yes");
                } else {
                    $ArchivePress->setFeedOption($feed_id, WP_ARCHIVEPRESS_PREFIX . "tryToFindExcerpt", "no");
                }

                // comment status
                $ArchivePress->setFeedOption($feed_id, WP_ARCHIVEPRESS_PREFIX . "defCommentsStatus", $_POST[WP_ARCHIVEPRESS_PREFIX . 'feedcommentstatus']);

                // activate feed (if needed)
                if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . 'feedactive'])) {
                    $ArchivePress->setFeed($feed_id, "active", "yes");
                    // then First Fetch
                    $ArchivePress->fetchFeed($ArchivePress->getFeed($feed_id));

                } else {
                    $ArchivePress->setFeed($feed_id, "active", "no");
                }

                // delete temporary options
                $ArchivePress->deleteTmpOption("name");
                $ArchivePress->deleteTmpOption("url");
                $ArchivePress->deleteTmpOption("protocol");
                $ArchivePress->deleteTmpOption("active");
                $ArchivePress->deleteTmpOption("interval");
                $ArchivePress->deleteTmpOption("comments");
                $ArchivePress->deleteTmpOption(WP_ARCHIVEPRESS_PREFIX . "followPermaLink");
                $ArchivePress->deleteTmpOption(WP_ARCHIVEPRESS_PREFIX . "embedMedia");
                $ArchivePress->deleteTmpOption(WP_ARCHIVEPRESS_PREFIX . "defCommentsStatus");
                $ArchivePress->deleteTmpOption(WP_ARCHIVEPRESS_PREFIX . "tryToFindExcerpt");
                $ArchivePress->deleteTmpOption(WP_ARCHIVEPRESS_PREFIX . "defPostStatus");
                $ArchivePress->deleteTmpOption(WP_ARCHIVEPRESS_PREFIX . "feedlinks");

                // if showWhatWillDo == yes
                // I have to show the what I will do before to do it...
                // the truth is that it's just a trick!
                if(get_option(WP_ARCHIVEPRESS_PREFIX . "showWhatWillDo") == ArchivePressOptions::YES) {
                    $ArchivePress->setTmpOption(WP_ARCHIVEPRESS_PREFIX . "intermediateid", $feed_id);
                    if($_POST[WP_ARCHIVEPRESS_PREFIX . 'feedfile'] != "") {
                        $ArchivePress->setTmpOption(WP_ARCHIVEPRESS_PREFIX . "feedfileuploaded", "yes");
                    }

                    // redirect to the proper url
                    wp_redirect(get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/add-feed-confirm.php');
                    exit;
                }

                $message = "&added=1";
            } else {
                // save temporary values (as options, to don't use SESSIONS)

                // name
                $ArchivePress->setTmpOption("name", $_POST[WP_ARCHIVEPRESS_PREFIX . 'feedname']);

                // url
                $ArchivePress->setTmpOption("url", $_POST[WP_ARCHIVEPRESS_PREFIX . 'feedurl']);

                // protocol
                $ArchivePress->setTmpOption("protocol", $_POST[WP_ARCHIVEPRESS_PREFIX . 'feedprotocol']);

                // fetching interval
                $ArchivePress->setTmpOption("interval", $_POST[WP_ARCHIVEPRESS_PREFIX . 'feedinterval']);

                // fetch also comments
                if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . 'feedcomments'])) {
                    $ArchivePress->setTmpOption("comments", "yes");
                } else {
                    $ArchivePress->setTmpOption("comments", "no");
                }

                // follow permalinks
                if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . 'feedpermalink'])) {
                    $ArchivePress->setTmpOption( WP_ARCHIVEPRESS_PREFIX . "followPermaLink", "yes");
                } else {
                    $ArchivePress->setTmpOption( WP_ARCHIVEPRESS_PREFIX . "followPermaLink", "no");
                }

                // embed media
                if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . 'feedmedia'])) {
                    $ArchivePress->setTmpOption( WP_ARCHIVEPRESS_PREFIX . "embedMedia", "yes");
                } else {
                    $ArchivePress->setTmpOption( WP_ARCHIVEPRESS_PREFIX . "embedMedia", "no");
                }

                // default post status
                $ArchivePress->setTmpOption( WP_ARCHIVEPRESS_PREFIX . "defPostStatus", $_POST[WP_ARCHIVEPRESS_PREFIX . 'feedpoststatus']);

                // excerpt
                if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . 'feedexcerpt'])) {
                    $ArchivePress->setTmpOption( WP_ARCHIVEPRESS_PREFIX . "tryToFindExcerpt", "yes");
                } else {
                    $ArchivePress->setTmpOption( WP_ARCHIVEPRESS_PREFIX . "tryToFindExcerpt", "no");
                }

                // comment status
                $ArchivePress->setTmpOption( WP_ARCHIVEPRESS_PREFIX . "defCommentsStatus", $_POST[WP_ARCHIVEPRESS_PREFIX . 'feedcommentstatus']);

                // activate feed (if needed)
                if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . 'feedactive'])) {
                    $ArchivePress->setTmpOption( "active", "yes");
                } else {
                    $ArchivePress->setTmpOption( "active", "no");
                }

                $message = "&error=".urlencode($lastSentence);
            }

            // switch back to the default feed protocol
            update_option(WP_ARCHIVEPRESS_PREFIX . "defFeedProtocol", $oldDefProtocol);

            break;

        case "addfeedconfirm":      // add a feed confirmation
            // security check
            check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'addfeedconfirm');

            // delete tmp options and settings
            $ArchivePress->deleteTmpOption(WP_ARCHIVEPRESS_PREFIX . "intermediateid");
            $ArchivePress->deleteTmpOption(WP_ARCHIVEPRESS_PREFIX . "feedfileuploaded");

            $message = "&added=1";
            break;

        case "settings": // save ArchivePress options
            // security check
            check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'settings');

            // Update ALL the settings
            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . "showWhatWillDo"])) {
                update_option(WP_ARCHIVEPRESS_PREFIX . "showWhatWillDo", "yes");
            } else {
                update_option(WP_ARCHIVEPRESS_PREFIX . "showWhatWillDo", "no");
            }

            update_option(WP_ARCHIVEPRESS_PREFIX . "htmlAllowedTags", $_POST[WP_ARCHIVEPRESS_PREFIX . "htmlAllowedTags"]);

            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . "saveContributors"])) {
                update_option(WP_ARCHIVEPRESS_PREFIX . "saveContributors", "yes");
            } else {
                update_option(WP_ARCHIVEPRESS_PREFIX . "saveContributors", "no");
            }

            update_option(WP_ARCHIVEPRESS_PREFIX . "defContributorLink", $_POST[WP_ARCHIVEPRESS_PREFIX . "defContributorLink"]);

            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . "keepPostAuth"])) {
                update_option(WP_ARCHIVEPRESS_PREFIX . "keepPostAuth", "yes");
            } else {
                update_option(WP_ARCHIVEPRESS_PREFIX . "keepPostAuth", "no");
            }

            update_option(WP_ARCHIVEPRESS_PREFIX . "defAuthor", $_POST[WP_ARCHIVEPRESS_PREFIX . "defAuthor"]);

            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . "keepPostCategories"])) {
                update_option(WP_ARCHIVEPRESS_PREFIX . "keepPostCategories", "yes");
            } else {
                update_option(WP_ARCHIVEPRESS_PREFIX . "keepPostCategories", "no");
            }

            update_option(WP_ARCHIVEPRESS_PREFIX . "defCategory", $_POST[WP_ARCHIVEPRESS_PREFIX . "defCategory"]);

            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . "useCategoriesAsTags"])) {
                update_option(WP_ARCHIVEPRESS_PREFIX . "useCategoriesAsTags", "yes");
            } else {
                update_option(WP_ARCHIVEPRESS_PREFIX . "useCategoriesAsTags", "no");
            }

            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . "tryAutoTag"])) {
                update_option(WP_ARCHIVEPRESS_PREFIX . "tryAutoTag", "yes");
            } else {
                update_option(WP_ARCHIVEPRESS_PREFIX . "tryAutoTag", "no");
            }

            update_option(WP_ARCHIVEPRESS_PREFIX . "YahooKey", $_POST[WP_ARCHIVEPRESS_PREFIX . "YahooKey"]);

            update_option(WP_ARCHIVEPRESS_PREFIX . "defFeedProtocol", $_POST[WP_ARCHIVEPRESS_PREFIX . "defFeedProtocol"]);

            update_option(WP_ARCHIVEPRESS_PREFIX . "defRssVersion", $_POST[WP_ARCHIVEPRESS_PREFIX . "defRssVersion"]);

            update_option(WP_ARCHIVEPRESS_PREFIX . "defAtomVersion", $_POST[WP_ARCHIVEPRESS_PREFIX . "defAtomVersion"]);

            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . "justAddedIsActive"])) {
                update_option(WP_ARCHIVEPRESS_PREFIX . "justAddedIsActive", "yes");
            } else {
                update_option(WP_ARCHIVEPRESS_PREFIX . "justAddedIsActive", "no");
            }

            update_option(WP_ARCHIVEPRESS_PREFIX . "defFeedFetchInt", $_POST[WP_ARCHIVEPRESS_PREFIX . "defFeedFetchInt"]);

            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . "postFetchComments"])) {
                update_option(WP_ARCHIVEPRESS_PREFIX . "postFetchComments", "yes");
            } else {
                update_option(WP_ARCHIVEPRESS_PREFIX . "postFetchComments", "no");
            }

            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . "followPermaLink"])) {
                update_option(WP_ARCHIVEPRESS_PREFIX . "followPermaLink", "yes");
            } else {
                update_option(WP_ARCHIVEPRESS_PREFIX . "followPermaLink", "no");
            }

            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . "embedMedia"])) {
                update_option(WP_ARCHIVEPRESS_PREFIX . "embedMedia", "yes");
            } else {
                update_option(WP_ARCHIVEPRESS_PREFIX . "embedMedia", "no");
            }

            update_option(WP_ARCHIVEPRESS_PREFIX . "defPostStatus", $_POST[WP_ARCHIVEPRESS_PREFIX . "defPostStatus"]);

            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . "tryToFindExcerpt"])) {
                update_option(WP_ARCHIVEPRESS_PREFIX . "tryToFindExcerpt", "yes");
            } else {
                update_option(WP_ARCHIVEPRESS_PREFIX . "tryToFindExcerpt", "no");
            }

            update_option(WP_ARCHIVEPRESS_PREFIX . "defCommentsStatus", $_POST[WP_ARCHIVEPRESS_PREFIX . "defCommentsStatus"]);

            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . "areCommentsApproved"])) {
                update_option(WP_ARCHIVEPRESS_PREFIX . "areCommentsApproved", "yes");
            } else {
                update_option(WP_ARCHIVEPRESS_PREFIX . "areCommentsApproved", "no");
            }

            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . "tryTypePadStandard"])) {
                update_option(WP_ARCHIVEPRESS_PREFIX . "tryTypePadStandard", "yes");
            } else {
                update_option(WP_ARCHIVEPRESS_PREFIX . "tryTypePadStandard", "no");
            }

            update_option(WP_ARCHIVEPRESS_PREFIX . "cronType", $_POST[WP_ARCHIVEPRESS_PREFIX . "cronType"]);

            update_option(WP_ARCHIVEPRESS_PREFIX . "defLogStatus", $_POST[WP_ARCHIVEPRESS_PREFIX . "defLogStatus"]);

            update_option(WP_ARCHIVEPRESS_PREFIX . "defLogLevel", $_POST[WP_ARCHIVEPRESS_PREFIX . "defLogLevel"]);

            $message = "&updated=true";
            break;

        default:exit;
    }
    // redirect to the proper url
    wp_redirect(get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/'.$_POST["back"].".php".$message);
}
?>